import controller from '../controller'

cc.Class({
	extends: controller,

	properties: {

	},

	onLoad () {
		controller.prototype.onLoad.call(this)
		cc.resources.load('data/serverList', (err, jsonAsset)=>{
		//cc.assetManager.loadRemote('http://mir2.com/serverList.json', (err, jsonAsset)=>{
			if(!err){
				this.view.serverListBg.active = true
				this.list = jsonAsset.json
				this.view.mask.active = false
				let groupBtn = this.view['serverListBg/groupList/groupBtn']
				this.view['serverListBg/groupList'].removeAllChildren()
				this.list.forEach((item, index)=> {
					let tmpBtn = cc.instantiate(groupBtn)
					tmpBtn.name = groupBtn.name + index
					tmpBtn.getComponentInChildren(cc.Label).string = item.name
					tmpBtn.y = groupBtn.y - index * 54
					tmpBtn.on('click', function (){this.onGroupBtn(index)}, this)
					this.view['serverListBg/groupList'].addChild(tmpBtn)
				})
				this.onGroupBtn(0)
			}
		})
		this.view.message.active = false
		this.view.panelLogin.active = false
		this.view.panelReg.active = false
		this.view.panelForget.active = false
	},

	start () {
		
	},

	onGroupBtn(groupIndex){
		let labels = this.view['serverListBg/groupList'].getComponentsInChildren(cc.Label)
		labels.forEach((item, index)=> {
			item.node.color = new cc.Color(255,255,255)
		})
		let groupBtn = this.view['serverListBg/groupList'].getChildByName('groupBtn'+groupIndex)
		groupBtn.getChildByName('label').color = new cc.Color(255,255,0)
		let serverBtn = this.view['serverListBg/serverList/serverBtn']
		this.view['serverListBg/serverList'].removeAllChildren()
		this.list[groupIndex].server.forEach((item, index)=> {
			let tmpBtn = cc.instantiate(serverBtn)
			tmpBtn.name = serverBtn.name + index
			tmpBtn.getComponentInChildren(cc.Label).string = item.name
			tmpBtn.x = index%2 == 0 ? tmpBtn.x : tmpBtn.x +  200
			tmpBtn.y = index%2 == 0 ? tmpBtn.y - index / 2 * 54 : tmpBtn.y - (index / 2 - 0.5) * 54
			tmpBtn.on('click', function (){this.onServerBtn(item)}, this)
			this.view['serverListBg/serverList'].addChild(tmpBtn)
		})
	},

	onServerBtn(serverInfo){
		this.view.serverListBg.active = false
		let doorAnimation = this.view['background/doorAnimation'].getComponent(cc.Animation)
		doorAnimation.play()
		doorAnimation.on(cc.Animation.EventType.FINISHED,function (){
			cc.director.loadScene('roleScene')
		})
	}
	// update (dt) {},
});
